﻿using Help;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;

namespace BLLA
{
    public partial class Order_MerSetWxMess_T : BaseBLL<Model.Order_MerSetWxMess_T>, IBLL.IOrder_MerSetWxMess_TBLL
    {
        ObjectSerialize os = new ObjectSerialize();
        DataResult result = new DataResult();
        /// <summary>
        /// 获取商户的所有微信通知号
        /// </summary>
        /// <param name="MerID">商户ID</param>
        /// <param name="YZM">验证码</param>
        /// <param name="RETID">数据格式(1:json;2:xml)</param>
        /// <returns></returns>
        public string GetMerWxID(string MerID, string YZM, int RETID)
        {
            #region 匹配验证码
            if (YZM != Help.ALL_DataV.YZM)
            {
                result.BS = "-88";
                result.Msg = "验证码错误！";
                return os.StringOfObject(result, RETID);
            }
            #endregion
            #region 业务层
            List<Model.View_Order_MerSetWxMess> mo = new List<Model.View_Order_MerSetWxMess>();
            try
            {
                mo = base.DBSession.IView_Order_MerSetWxMessDAL.GetListBy("SELECT * FROM View_Order_MerSetWxMess where MerID='" + MerID + "'");
            }
            catch (Exception)
            {
                result.BS = "-1";
                result.Msg = "查询不到数据！";
                return os.StringOfObject(result, RETID);
            }
            result.BS = "1";
            result.Msg = "查询成功！";
            result.total = mo.Count;
            result.rows = mo;
            return os.StringOfObject(result, RETID);
            #endregion
        }


        public string UpdateMerWxID(string json, int tag, string YZM, int RETID)
        {
            #region 匹配验证码
            if (YZM != Help.ALL_DataV.YZM)
            {
                result.BS = "-88";
                result.Msg = "验证码错误！";
                return os.StringOfObject(result, RETID);
            }
            #endregion
            #region 执行操作
            try
            {
                Model.Order_MerSetWxMess_T info = ParseClass<Model.Order_MerSetWxMess_T>.StrDeserialize(json, RETID);
                string T = "";
                int ret = -1;
                // 根据操作标签Tag判断是什么操作
                switch (tag)
                {
                    case 1:
                        ret = base.Add(info);
                        break;
                    case 2:
                        Model.Order_MerSetWxMess_T tmp = base.GetListBy(u => u.ID == info.ID).First();
                        Help.FuZhi<Model.Order_MerSetWxMess_T>.FZ(ref tmp, info);
                        ret = base.Modify(tmp);
                        break;
                    case 3:
                        tmp = base.GetListBy(u => u.ID == info.ID).First();
                        tmp.Stat = 0;
                        ret = base.Modify(tmp);
                        break;
                }
                if (ret > 0)
                {
                    result.BS = "1";
                    result.Msg = "更新成功！";
                }
                else
                {
                    result.BS = "-1";
                    result.Msg = "更新失败！";
                }
            }
            catch (Exception ex)
            {
                WriteLog.WriteW("更新商品分类信息失败：" + ex.Message);
                result.BS = "-1";
                result.Msg = "更新失败";
            }
            #endregion
            return os.StringOfObject(result, RETID);
        }

        /// <summary>
        /// 增删改微信通知号
        /// </summary>
        /// <param name="json">json数据</param>
        /// <param name="tag">1-增, 2-改, 3-删</param>
        /// <param name="YZM">验证码</param>
        /// <param name="RETID">数据格式(1:json;2:xml)</param>
        /// <returns></returns>
        public string Set_MerWxID(string json, int tag, string YZM, int RETID)
        {
            #region 匹配验证码
            if (YZM != Help.ALL_DataV.YZM)
            {
                result.BS = "-88";
                result.Msg = "验证码错误！";
                return os.StringOfObject(result, RETID);
            }
            #endregion
            #region 执行操作
            try
            {
               //Model.Order_MerSetWxMess_T merse = new Model.Order_MerSetWxMess_T() { MerID="2",OpenName="薛妹",WxMesCall="18977432115",OpenID="",ID=6};
               // json = os.StringOfObject(merse, 1);
                Model.Order_MerSetWxMess_T info = ParseClass<Model.Order_MerSetWxMess_T>.StrDeserialize(json, RETID);
                string T = "";
                int ret = -1;
                // 根据操作标签Tag判断是什么操作
                switch (tag)
                {
                    case 1://1-增
                        string chkCode = "";
                        char[] character = { '0', '1', '2', '3', '4', '5', '6', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
                        Random rnd = new Random();
                        bool bo = true;
                        string yzm = "";
                       
                    //生成验证码字符串 ,遍历这个验证码在数据库中是否有重
                        while (bo)
                        {
                            for (int i = 0; i < 6; i++)
                            {
                                chkCode += character[rnd.Next(character.Length)]; //验证码
                            }
                            yzm = "TZY" + chkCode;
                            string time = DateTime.Now.ToString("yyyyMMddHHmmss");
                            List<Model.Order_MerSetWxMess_T> omt = base.DBSession.IOrder_MerSetWxMess_TDAL.GetListBy(u => u.YZM==yzm & u.Stat==1).ToList();
                            if (omt.Count <= 0)
                            {
                                bo = false;
                            }
                        }
                        info.OpenID = "";
                        info.YZM = "TZY" + chkCode;
                        string yzmtime = DateTime.Now.AddDays(1).ToString("yyyyMMddHHmmss"); //当前时间,精确到秒
                        info.YZMTime = yzmtime;
                        info.Stat = 1;
                        info.OpenID = "";
                        ret = base.Add(info);
                        break;
                    case 2://2-改
                        Model.Order_MerSetWxMess_T tmp = base.GetListBy(u => u.ID == info.ID).First();
                        if(info.OpenID ==null)
                        {
                            info.OpenID = "";
                        }
                        Help.FuZhi<Model.Order_MerSetWxMess_T>.FZ(ref tmp, info);
                        ret = base.Modify(tmp);
                        break;
                    case 3://3-删
                        tmp = base.GetListBy(u => u.ID == info.ID).First();
                        if (info.OpenID == null)
                        {
                            info.OpenID = "";
                        }
                        tmp.Stat = 0;
                        ret = base.Modify(tmp);
                        break;
                }
                if (ret > 0)
                {
                    result.BS = "1";
                    result.Msg = "更新成功！";
                }
                else
                {
                    result.BS = "-1";
                    result.Msg = "更新失败！";
                }
            }
            catch (Exception ex)
            {
                WriteLog.WriteW("更新商品分类信息失败：" + ex.Message);
                result.BS = "-1";
                result.Msg = "更新失败";
            }
            #endregion
            return os.StringOfObject(result, RETID);
        }

    }
}
